//
//  CommentsTableViewCell.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/4.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "CommentsTableViewCell.h"

@implementation CommentsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor clearColor];
        
        /**
         头像
         */
        
        UIButton *iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [iconBtn setImage:[UIImage imageNamed:@"未登录头像新"] forState:UIControlStateNormal];
        iconBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        iconBtn.imageView.layer.masksToBounds = YES;
        iconBtn.layer.cornerRadius = 17.5;
        iconBtn.clipsToBounds = YES;
        [self addSubview:iconBtn];
        self.iconBtn = iconBtn;
        
        /**
         名字
         */
        
        UILabel *nameLable = [[UILabel alloc]init];
        nameLable.text = @"这是一个名字";
        nameLable.textColor = CSColorFromRGB(0x3297EC);
        nameLable.font = [UIFont systemFontOfSize:14];
        self.nameLable = nameLable;
        [self addSubview:nameLable];
        
        
        /**
         时间
         */
        UILabel *timeLable = [[UILabel alloc]init];
        timeLable.text = @"2018-11-09 18:59";
        timeLable.textColor = CSColorFromRGB(0x6B6763);
        timeLable.font = [UIFont systemFontOfSize:12];
        self.timeLable = timeLable;
        [self addSubview:timeLable];
        
        /**
         赞数
         */
        UIButton *zanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [zanBtn setImage:[UIImage imageNamed:@"点赞"] forState:UIControlStateNormal];
        [zanBtn setImage:[UIImage imageNamed:@"已点赞拷贝"] forState:UIControlStateSelected];
        [zanBtn setTitle:@"2800" forState:UIControlStateNormal];
        [zanBtn setTitleColor:CSColorFromRGB(0x6B6763) forState:UIControlStateNormal];
        zanBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [self addSubview:zanBtn];
        self.zanBtn = zanBtn;
        
        /**
         内容
         */
        /**
         全部
         */
        UILabel *contentLable = [[UILabel alloc]init];
        contentLable.text = @"香菜沐浴露是一种什么样的体验香菜沐浴露是一种什么样的体验？香菜沐浴露是一种什么样的体验香菜沐浴露是一种什么样的体验？";
        contentLable.textColor = CSBlackColor1;
        contentLable.font = [UIFont systemFontOfSize:15];
        contentLable.numberOfLines = 6;
        self.contentLable = contentLable;
        [self addSubview:contentLable];
        
        [self setConstraints];
        
    }
    return self;
}

-(void)setConstraints
{
    //设置约束条件
    
    /**
     头像
     */
    [self.iconBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(15);
        make.left.mas_equalTo(self).offset(12);
        make.height.width.mas_equalTo(35);
        
    }];
    
    /**
     名字
     */
    [self.nameLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(15);
        make.left.mas_equalTo(self.iconBtn.mas_right).offset(10);
        make.right.mas_lessThanOrEqualTo(-70);
        
    }];
    
    /**
     时间
     */
    [self.timeLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.nameLable.mas_bottom).offset(8);
        make.left.mas_equalTo(self.nameLable);
        
    }];
    
    
    /**
     赞数
     */
    
    [self.zanBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.nameLable);
        make.right.mas_equalTo(self).offset(-13);
        make.height.mas_equalTo(20);
        
    }];
    
    /**
     内容
     */
    [self.contentLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.nameLable);
        make.right.mas_equalTo(self).offset(-13);
        make.top.mas_equalTo(self.iconBtn.mas_bottom).offset(8);
        make.bottom.mas_equalTo(self).offset(-10);
        
    }];
}

-(void)setTitleString:(NSString *)titleString
{
    _titleString = titleString;
    self.contentLable.text = titleString;
    NSArray *arr =  [self getLinesArrayOfStringInLabel:self.contentLable];
    NSString *result = arr.count <= 6 ? titleString : [self getStringForLast:arr numberofline:6];
    
    self.contentLable.text = result;
    
}

-(void)layoutSubviews
{
    [self.zanBtn imageOnTheTitleLeftWithSpace:5];
    
    
    
}

-(NSString *)getStringForLast:(NSArray *)lableArr numberofline:(NSInteger )number
{
    NSString *result;
    
    if (lableArr.count > number) {
        NSString *text1 = lableArr[number-1];
        NSString *b;
        
        if (text1.length <2) {
            return nil;
        }
        
        b = [text1 substringFromIndex:text1.length -8];
        
        for (int i = 0; i < number; i ++) {
            
            if (i == number-1) {
                [b stringByAppendingString:@"...[查看更多]"];
                [result stringByAppendingString:b];
            }else
            {
                [result stringByAppendingString:lableArr[i]];
            }
                
        }
        
    }
    
    return result;
}


- (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label{
    NSString *text = [label text];
    UIFont *font = [label font];
    CGRect rect = [label frame];
    
    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)myFont range:NSMakeRange(0, attStr.length)];
    CFRelease(myFont);
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];
    for (id line in lines) {
        CTLineRef lineRef = (__bridge  CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        NSString *lineString = [text substringWithRange:range];
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));
        //NSLog(@"''''''''''''''''''%@",lineString);
        [linesArray addObject:lineString];
    }
    
    CGPathRelease(path);
    CFRelease( frame );
    CFRelease(frameSetter);
    return (NSArray *)linesArray;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end



